﻿using System;
using Gemama;
using CounterSoft.Gemini.WebServices;
using System.Configuration;
using CounterSoft.Gemini.Commons.Rest;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["help"] != null)
            {
                this.Help();
            }
            else
            {
                if (Request.Params["reminder"] != null)
                {
                    this.Reminder();
                }
                else
                {
                    this.ProcessMails();
                }
            }
        }

        private void Reminder()
        {
            Engine gemamaEngine = new Engine();
            Gemama.Engineresult res = gemamaEngine.Reminder();
            this.LitResultCode.Text = res.ResultCode.ToString();
            this.LitResultString.Text = res.ResultString;
            this.LitIssuesOK.Text = res.IssuesOK.ToString();
            this.LitIssuesError.Text = res.IssuesError.ToString();
            this.LitCommentsOK.Text = res.CommentsOK.ToString();
            this.LitCommentsError.Text = res.CommentsError.ToString();
            this.LitReminderAssigneeMails.Text = res.ReminderAssigneeMail.ToString();
            this.LitReminderProjectMails.Text = res.ReminderProjectMail.ToString();
            this.TotalErrors.Text = (res.IssuesError + res.CommentsError + res.ResultCode).ToString();
        }

        private void Help()
        {
            ServiceManager serviceManager = this.GeminiInitialize();
            CounterSoft.Gemini.Commons.Entity.IssueRiskEN[] riskEN = serviceManager.AdminService.GetIssueRisk();
            CounterSoft.Gemini.Commons.Entity.IssueTypeEN[] typeEN = serviceManager.AdminService.GetIssueType();
            CounterSoft.Gemini.Commons.Entity.IssueStatusEN[] statusEN = serviceManager.AdminService.GetIssueStatus();
            CounterSoft.Gemini.Commons.Entity.IssuePriorityEN[] priorityEN = serviceManager.AdminService.GetIssuePriority();
            CounterSoft.Gemini.Commons.Entity.IssueSeverityEN[] severityEN = serviceManager.AdminService.GetIssueSeverity();

            Response.Write("Risk-IDs (use this for 'NewIssueRisklevel')<br />");
            Response.Write("================<br />");
            foreach (CounterSoft.Gemini.Commons.Entity.IssueRiskEN item in riskEN)
            {
                Response.Write(item.ID + "=" + item.Description + "<br />");
            }

            Response.Write("<br />");
            Response.Write("Type-IDs (use this for 'NewIssueIssueType')<br />");
            Response.Write("================<br />");
            foreach (CounterSoft.Gemini.Commons.Entity.IssueTypeEN item in typeEN)
            {
                Response.Write(item.ID + "=" + item.Description + "<br />");
            }

            Response.Write("<br />");
            Response.Write("Status-IDs (use this for 'NewIssueIssueStatus')<br />");
            Response.Write("================<br />");
            foreach (CounterSoft.Gemini.Commons.Entity.IssueStatusEN item in statusEN)
            {
                Response.Write(item.StatusID + "=" + item.Description + "<br />");
            }

            Response.Write("<br />");
            Response.Write("Priority-IDs (use this for 'NewIssueIssuePriority')<br />");
            Response.Write("================<br />");
            foreach (CounterSoft.Gemini.Commons.Entity.IssuePriorityEN item in priorityEN)
            {
                Response.Write(item.ID + "=" + item.Description + "<br />");
            }

            Response.Write("<br />");
            Response.Write("Severity-IDs (use this for 'NewIssueIssueSeverity')<br />");
            Response.Write("================<br />");
            foreach (CounterSoft.Gemini.Commons.Entity.IssueSeverityEN item in severityEN)
            {
                Response.Write(item.ID + "=" + item.Description + "<br />");
            }

            this.resblock.Visible = false;
        }

        private void ProcessMails()
        {
            Engine gemamaEngine = new Engine();
            Gemama.Engineresult res = gemamaEngine.Compute();
            this.LitResultCode.Text = res.ResultCode.ToString();
            this.LitResultString.Text = res.ResultString;
            this.LitIssuesOK.Text = res.IssuesOK.ToString();
            this.LitIssuesError.Text = res.IssuesError.ToString();
            this.LitCommentsOK.Text = res.CommentsOK.ToString();
            this.LitCommentsError.Text = res.CommentsError.ToString();
            this.LitReminderAssigneeMails.Text = res.ReminderAssigneeMail.ToString();
            this.LitReminderProjectMails.Text = res.ReminderProjectMail.ToString();
            this.TotalErrors.Text = (res.IssuesError + res.CommentsError + res.ResultCode).ToString();
        }

        private ServiceManager GeminiInitialize()
        {
            ServiceManager serviceManager = null;
            try
            {
                serviceManager = new ServiceManager(
                    ConfigurationSettings.AppSettings["GeminiServerURL"],
                    ConfigurationSettings.AppSettings["GeminiUser"],
                   ConfigurationSettings.AppSettings["GeminiPassword"],
                   ConfigurationSettings.AppSettings["GeminiAPIkey"],
                    bool.Parse(ConfigurationSettings.AppSettings["GeminiWindowsAuthenticationMode"]));
            }
            catch (GeminiRestException ex1)
            {
                Console.WriteLine(ex1.Message);
            }
            catch (Exception ex2)
            {
                Console.WriteLine(ex2.Message);
            }

            return serviceManager;
        }
    }
}
